package com.lcf.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.lcf.framework.exception.SysException;
import com.lcf.framework.utils.UserUtils;
import com.lcf.system.dao.SysTenantDao;
import com.lcf.system.dto.tenant.SysTenantDTO;
import com.lcf.system.dto.tenant.SysTenantEditDTO;
import com.lcf.system.entity.SysTenant;
import com.lcf.system.service.SysTenantService;
import com.lcf.system.vo.tenant.SysTenantVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 租户(SysTenant)表服务实现类
 *
 * @author lcf
 * @since 2025-03-02 23:00:13
 */
@Service("sysTenantService")
public class SysTenantServiceImpl extends ServiceImpl<SysTenantDao, SysTenant> implements SysTenantService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SysTenantDTO dto) {
        boolean exists = Db.lambdaQuery(SysTenant.class).eq(SysTenant::getTenantName, dto.getTenantName()).exists();
        if (exists) {
            throw new SysException("社区名称已存在");
        }

        BeanUtil.copyProperties(dto, SysTenant.class).insert();
    }

    @Override
    public void edit(SysTenantEditDTO dto) {
        SysTenant sysTenant = BeanUtil.copyProperties(dto, SysTenant.class);
        sysTenant.setId(UserUtils.getTenantId());
        sysTenant.updateById();
    }

    @Override
    public List<SysTenantVO> search(String tenantName) {
        List<SysTenant> list = Db.lambdaQuery(SysTenant.class).like(SysTenant::getTenantName, tenantName).list();
        return BeanUtil.copyToList(list, SysTenantVO.class);
    }
    @Override
    public List<SysTenantVO> getList() {
        List<SysTenant> list = Db.lambdaQuery(SysTenant.class).list();
        return BeanUtil.copyToList(list, SysTenantVO.class);
    }

}

